package com.ssm.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ssm.auth.common.enums.IsDeletedFlagEnum;
import com.ssm.auth.domain.bo.AuthPermissionBO;
import com.ssm.auth.domain.convert.AuthPermissionBoConvert;
import com.ssm.auth.domain.redis.RedisUtil;
import com.ssm.auth.domain.service.AuthPermissionDomainService;
import com.ssm.auth.infra.basic.entity.AuthPermission;
import com.ssm.auth.infra.basic.service.AuthPermissionService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Log4j2
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private AuthPermissionService authPermissionService;

    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";

    /**
     * 新增权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()) {
            log.info("PermissionController.add.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBoConvert.INSTANCE.AuthPermissionBoToPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = authPermissionService.insert(authPermission);
        return count > 0;
    }

    /**
     * 更新权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()) {
            log.info("PermissionController.update.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBoConvert.INSTANCE.AuthPermissionBoToPermission(authPermissionBO);
        int count = authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 删除权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if(log.isInfoEnabled()) {
            log.info("PermissionController.delete.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBoConvert.INSTANCE.AuthPermissionBoToPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 查询用户权限
     * @param userName
     * @return
     */
    @Override
    public List<String> getPermission(String userName) {
        String permissionKey = redisUtil.buildKey(authPermissionPrefix, userName);
        String permissionValue = redisUtil.get(permissionKey);

        if(StringUtils.isBlank(permissionValue)) {
            return Collections.emptyList();
        }
        //JSON 转为 List
        List<AuthPermission> permissionList = new Gson().fromJson(permissionValue,
                new TypeToken<List<AuthPermission>>() {
                }.getType());
        List<String> collect = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return collect;
    }
}
